package com.epam.edu.task01.tourism;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import com.epam.edu.task01.tourism.voucher.VouchersList;

public class Program {

	private static Logger logger = Logger.getLogger(Program.class);

	public static void main(String[] args) throws Exception {
		BasicConfigurator.configure();
		DataInputStream in = null;
		VouchersList vouchersList = new VouchersList();
		try {
			FileInputStream fstream = new FileInputStream("1.txt");
			in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String inputString;
			int i=1;
			while ((inputString = br.readLine()) != null) {
				if (inputString.indexOf("|") >= 0) {
					String voucherType = inputString.substring(0,
							inputString.indexOf("|"));
					String voucherData = inputString.substring(
							inputString.indexOf("|") + 1, inputString.length()
									- inputString.indexOf("|"));
					vouchersList.addVoucher(voucherType, voucherData);
					// System.out.println(inputString);
				} else {
					logger.info("Number "+Integer.toString(i)+ " in a row incorrectly entered data");
				}
				i++;
			}
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			in.close();
		}
		vouchersList.toString();
	}
}
